//
//  XinLiCePingOptionsView.m
//  students
//
//  Created by 任辰东 on 2024/3/4.
//

#import "XinLiCePingOptionsView.h"
#import "XinLiCePingOptionsTCell.h"

@interface XinLiCePingOptionsView ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UIView *wrapView;
@property (nonatomic, assign) BOOL isCompleteAnimation;

@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) NSMutableArray <XinLiCePingOptionsModel *>*dataSourceArr;

/**
 当数据为空时 占位图(使用时要在刷新前创建)
 */
@property (nonatomic, strong) RCDEmptyView *emptyView;


@property (nonatomic, strong) XinLiCePingModel *model;
@end

@implementation XinLiCePingOptionsView

-(void)configViewWithModel:(XinLiCePingModel *)model
{
    self.model = model;
    
    NSMutableArray *dataArray = [NSMutableArray arrayWithArray:model.options];
    for (NSInteger i = 0; i < dataArray.count; i ++) {
        XinLiCePingOptionsModel *options = dataArray[i];
        if ([model.daan isEqualToString:options.options_name]) {
            options.is_answer = YES;
        }else{
            options.is_answer = NO;
        }
        [dataArray replaceObjectAtIndex:i withObject:options];
    }
    
    self.dataSourceArr = [NSMutableArray arrayWithArray:dataArray];
    
    [self.myTableView mas_remakeConstraints:^(MASConstraintMaker *make) {

        make.top.right.equalTo(self.wrapView);
        make.left.right.equalTo(self.wrapView);
        make.width.mas_equalTo(kScreenWidth);
        make.bottom.equalTo(self.wrapView).offset(0);
        make.height.mas_equalTo(30.0 * self.dataSourceArr.count);
    }];
    [self.myTableView reloadData];
}

-(void)setupViews
{
    [self addSubview:self.wrapView];
    [self.wrapView addSubview:self.myTableView];
    
    [self.myTableView registerClass:[XinLiCePingOptionsTCell class] forCellReuseIdentifier:NSStringFromClass([XinLiCePingOptionsTCell class])];
}

-(void)setupLayout
{
    
    [self.wrapView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.edges.equalTo(self);
    }];

    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.right.equalTo(self.wrapView);
        make.left.right.equalTo(self.wrapView);
        make.width.mas_equalTo(kScreenWidth);
        make.bottom.equalTo(self.wrapView).offset(0);
    }];
}

-(void)addEmptyView
{
    if (IsEmpty(self.dataSourceArr)) {
        [RCDEmptyView addEmptyWithStyle:EmptyViewStyleCommon onView:self.myTableView position:EmptyViewPositionCenter clickCallBack:^{
        }];;
    }else{
        [RCDEmptyView removeFromParentView:self.myTableView];
    }
}

#pragma mark - tableView delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 30.0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XinLiCePingOptionsModel *model;
    if (indexPath.row < self.dataSourceArr.count) {
        model = self.dataSourceArr[indexPath.row];
    }
    XinLiCePingOptionsTCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XinLiCePingOptionsTCell class])];
    [cell configCellWithModel:model];
    cell.backgroundColor = [UIColor whiteColor];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (self.model.is_answer.boolValue) {
     
        return;
    }
    
    XinLiCePingOptionsModel *model;
    if (indexPath.row < self.dataSourceArr.count) {
        model = self.dataSourceArr[indexPath.row];
    }
    
    for (XinLiCePingOptionsModel *model in self.dataSourceArr) {
        model.is_answer = NO;
    }
    model.is_answer = YES;
    [self.myTableView reloadData];
    
    if (self.SelectedBlock) {
        self.SelectedBlock(model);
    }
    
}

//此方法TableView的分割线顶边
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}
#pragma mark -- lazyLoad

-(UIView *)wrapView
{
    if (!_wrapView) {
        _wrapView = [UIView new];
        _wrapView.backgroundColor = UIColorFromHexWithAlpha(0xffffff, 1);
    }
    return _wrapView;
}

- (UITableView *)myTableView
{
    if (!_myTableView) {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _myTableView.estimatedRowHeight = 40;//根据需要重写
        _myTableView.showsVerticalScrollIndicator = NO;
        _myTableView.allowsSelection = YES;
        _myTableView.allowsMultipleSelection = NO;
//        _myTableView.scrollEnabled = NO;
        
        //此方法TableView的分割线顶边
        if ([self.myTableView respondsToSelector:@selector(setSeparatorInset:)])
        {
            [self.myTableView setSeparatorInset:UIEdgeInsetsZero];
        }
        
        if ([self.myTableView respondsToSelector:@selector(setLayoutMargins:)])
        {
            [self.myTableView setLayoutMargins:UIEdgeInsetsZero];
        }
    }
    return _myTableView;
}

-(NSMutableArray<XinLiCePingOptionsModel *> *)dataSourceArr
{
    if (!_dataSourceArr) {
        _dataSourceArr = [NSMutableArray array];
    }
    return _dataSourceArr;
}

@end
